import { lastValueFrom } from "rxjs";
import { Injectable } from "@nestjs/common";

import {
  Action,
  Contact,
  RestDto,
  ENUM_APP,
  ContactGroup,
  findManyContactRpcDto,
  ContactRpcInsertOnePath,
  putOneContactSyncRpcDto,
  findOneContactByIdRpcDto,
  ContactRpcFindOneByIdPath,
  ContactRpcFindManyByIdPath,
  ContactRpcInterfaceWithDerive,
  ContactRpcGetHighestGroupSyncPath,
} from "qqlx-core";
import {} from "qqlx-cdk";

import { BusRpc } from "./app.droplet-bus";
import { RpcWaiting } from "./rpc.response";
import { RpcConnection } from "./com.connection";

@Injectable()
export class ContactRpc extends RpcConnection implements ContactRpcInterfaceWithDerive<RestDto> {
  constructor(readonly BusRpc: BusRpc) {
    super(BusRpc, ENUM_APP.POND_CONTACT);
  }

  @RpcWaiting(ContactRpcGetHighestGroupSyncPath)
  getHighestGroupSync(dto: RestDto) {
    const ob = this.CLIENT.send(ContactRpcGetHighestGroupSyncPath, dto);
    return lastValueFrom<{ group: ContactGroup }>(ob);
  }

  @RpcWaiting(ContactRpcInsertOnePath)
  putOneSync(dto: RestDto & putOneContactSyncRpcDto) {
    const ob = this.CLIENT.send(ContactRpcInsertOnePath, dto);
    return lastValueFrom<Action<Contact>>(ob);
  }

  @RpcWaiting(ContactRpcFindOneByIdPath)
  findOne(dto: RestDto & findOneContactByIdRpcDto) {
    const ob = this.CLIENT.send(ContactRpcFindOneByIdPath, dto);
    return lastValueFrom<Contact>(ob);
  }

  @RpcWaiting(ContactRpcFindManyByIdPath)
  findMany(dto: RestDto & findManyContactRpcDto) {
    const ob = this.CLIENT.send(ContactRpcFindManyByIdPath, dto);
    return lastValueFrom<Contact[]>(ob);
  }
}
